package controllers;

import play.*;
import play.libs.Codec;
import play.mvc.*;

import java.util.*;

import notifiers.Mails;

import models.*;

public class Application extends Controller {
	
	/**
	 * zum ersten laden der Seite.
	 * @param initialvwenn nach Anfangsbuchstaben von Events gesucht wird.
	 */
    public static void index(String selection, EventCategory cat, String initial) {
    	final String[] firstletters = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}; 
    	User user = Security.getConnected();
    	List<Event> preselectedEvents = null;
    	
    	List<String> dropdownEventList = Search.dropdownEventList();
    	
    	if(selection==null){
    		selection="upcoming";
    	}
    	
    	
    	if(selection.equals("popular")) {
    		preselectedEvents = Search.searchForPopularEvents(cat, initial, 5);
    	}
    	else {
    		preselectedEvents = Search.searchForUpcomingEvents(cat, initial, 5);
    	}
 	
    	render(selection, cat, initial, firstletters, dropdownEventList, preselectedEvents, user);
    }
    
	public static void showEvents(String name, float fromPrice, float toPrice, boolean includePrivateOffers ) {
		
		User user = Security.getConnected();
				
		List<Event> events = Search.searchEventByName(name);
		
		renderArgs.put("includePrivateOffers", includePrivateOffers);
		renderArgs.put("fromPrice", fromPrice);
		renderArgs.put("toPrice", toPrice);
		
		flash.put("fromPrice", fromPrice);
		flash.put("toPrice", toPrice);
		flash.put("includePrivate", includePrivateOffers);		
				
		if(events.size()==1) {
			showOffers(events.get(0).id, fromPrice, toPrice, includePrivateOffers);
		}
		else {
			render(name, events, user);
		}
		
		
	}
    
    
    
	/**
	 * um die Offers zu einem Event anzuzeigen.
	 * @param id event id
	 * @param fromPrice
	 * @param toPrice
	 * @param includePrivateOffers
	 */	
	public static void showOffers(Long id, float fromPrice, float toPrice, boolean includePrivateOffers){
		User user = Security.getConnected();
		
		List<Offer> offerList = Search.searchForOffers(id, fromPrice, toPrice, includePrivateOffers, false);
		List<Offer> premiumOfferList = Search.searchForOffers(id, fromPrice, toPrice, includePrivateOffers, true);
		
		renderArgs.put("event", Event.findById(id));
		renderArgs.put("includePrivateOffers", includePrivateOffers);
		renderArgs.put("fromPrice", fromPrice);
		renderArgs.put("toPrice", toPrice);
		
		flash.put("fromPrice", fromPrice);
		flash.put("toPrice", toPrice);
		flash.put("includePrivate", includePrivateOffers);
		
		render(offerList, premiumOfferList, user);
		
	}
	 
	 public static void faq() {
		User user = Security.getConnected();
		render(user);
	}	 
	 
	 public static void aboutUs() {
		User user = Security.getConnected();
	    render(user);
	 }
	 
	 public static void login() {
		User user = Security.getConnected();
		render(user);
	 }

	 public static void lostPassword() {
		 render();
	 }
	 
	 public static void sendNewPassword(String email) {
		 User user = User.findByEmail(email);
		 email = "";
		 String firstname = "";
		 String lastname = "";
		 
		 if(user!=null) {			 
			 String newPassword = Codec.UUID(); 
			 user.passwordHash = Codec.hexMD5(newPassword);	
			 user.save();
			 Mails.lostPassword(user, newPassword);
			 email = user.email;
			 firstname = user.firstname;
			 lastname = user.lastname;
		 }
		 
		 render(email, firstname, lastname);
	 }

}
